<?php
/**
 * The machines class.
 * @author Jamie Dockendorff <j.dockendorff@gmail.com>
 * @version 1.0
 * @since 2013-10-30
 */
require_once 'database.php';
require_once 'table.php';

class machine extends table {
	const STATUS_RUNNING = 0;
	const STATUS_IDLE = 1;
	const STATUS_MAINTENANCE = 2;

	var $name;
	var $type;
	var $last_maintenance;
	var $time_between_maintenances;
	var $cost;
	var $status;

	/**
	 * The constructor of the machine class.
	 *
	 * @param i		The ID of this machine.
	 * @return		The newly initialized machine object.
	 */
	function __construct($i) {
		parent::__construct ($i,'Machines');
		$this->name							= "";
		$this->type							= -1;
		$this->last_maintenance				= -1;
		$this->time_between_maintenances	= -1;
		$this->cost							= -1;
		$this->status						= -1;
	}

	/**
	 * Initialize this object from its data.
	 *
	 * @param nam			The name of this machine.
	 * @param typ			The type of machine that this one is.
	 * @param lastmain		The last time that this machine underwent maintenance.
	 * @param timebtmain	The time between maintenances for this machine.
	 * @param cos			The amount of money spent by running this machine.
	 * @param stat			The status of this machine.
	 */
	function init_from_data($nam,$typ,$lastmain,$timebtmain,$cos,$stat) {
		$this->name							= $nam;
		$this->type							= $typ;
		$this->last_maintenance				= $lastmain;
		$this->time_between_maintenances	= $timebtmain;
		$this->cost							= $cos;
		$this->status						= $stat;
	}

	/**
	 * Initialize this machine object from form data.
	 *
	 * @param post	The _POST variable from a form.
	 * @return		None.
	 */
	function init_from_post($post) {
		if (!(isset($post['name'])
			 && isset($post['type'])
			 && isset($post['last_maintenance'])
			 && isset($post['time_between_maintenances'])
			 && isset($post['cost']) && isset($post['status'])))
			return FALSE;

		$this->init_from_data (
			$post['name'], $post['type'],
			$post['last_maintenance'],
			$post['time_between_maintenances'],
			$post['cost'], $post['status']
		);
		return TRUE;
	}

	/**
	 * Update the last maintenance time of this machine.
	 *
	 * @param db	The database object connected to the database.
	 * @param time	The time, or 0 for the current time.
	 */
	function update_maint ($db, $time = 0) {
		if ($time == 0) {
			$time = time ();
		}
		$this->last_maintenance = $time;
		$result = $this->update_database_by_id ($db);
		return $result;
	}

	/**
	 * Update the status of this machine.
	 *
	 * @param db		The database object connected to the database.
	 * @param status	The new status of this machine.
	 */
	function update_status ($db, $status) {
		$this->status = $status;
		$result = $this->update_database_by_id ($db);
		return $result;
	}

	/**
	 * Prints a form to select a machine from the database.
	 *
	 * @param action	The action to perform on submission.
	 */
	static function select ($action) {
		$db = new database ();
		$machines = $db->query ("SELECT id, name FROM Machines");
		echo "<form name=\"employee\" action=\"" . $action . "\" method=\"post\">\n";
		echo "<select name=\"name\">\n";
		while ($row = $machines->fetch_assoc ()) {
			$id = $row['id']; $name = $row['name'];
			echo "<option value='$id'>$name</option>\n";
		}
		echo "</select><br>";
		echo "<input type=\"submit\" value=\"Go\"><br>\n";
		echo "</form><br>\n";
	}

}

?>